package GUILayer;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import ControlLayer.CustomerCtr;
import ControlLayer.OrderCtr;
import ModelLayer.Customer;
import ModelLayer.SalesOrder;

public class CustomerMenu
{
	private static CustomerMenu instance=null;
	public JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTable table;
	private JTextField textField_6;
	
	public CustomerMenu()
	{
		initialize();
		frame.setVisible(true);
	}
	
	public static CustomerMenu getInstance()
	{
		if(instance==null)
		{
			instance= new CustomerMenu();
		}
		return instance;
	}

	private void initialize()
	{
		frame = new JFrame();
		frame.setBounds(100, 100, 950, 530);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setUndecorated(true);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "Customer data", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 765, 159);
		frame.getContentPane().add(panel);
		
		JLabel lblCustomerId = new JLabel("Customer ID:");
		lblCustomerId.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCustomerId.setBounds(6, 16, 100, 25);
		panel.add(lblCustomerId);
		
		JLabel label_1 = new JLabel("Name:");
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		label_1.setBounds(6, 52, 100, 25);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("Address:");
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		label_2.setBounds(6, 88, 100, 25);
		panel.add(label_2);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCity.setBounds(6, 124, 100, 25);
		panel.add(lblCity);
		
		JLabel lblZipcode = new JLabel("Zipcode:");
		lblZipcode.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblZipcode.setBounds(415, 16, 100, 25);
		panel.add(lblZipcode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPhone.setBounds(415, 52, 100, 25);
		panel.add(lblPhone);
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(96, 20, 240, 20);
		panel.add(textField);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(96, 56, 240, 20);
		panel.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(96, 88, 240, 20);
		panel.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(96, 128, 240, 20);
		panel.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(515, 20, 240, 20);
		panel.add(textField_4);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(515, 56, 240, 20);
		panel.add(textField_5);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblEmail.setBounds(415, 88, 100, 25);
		panel.add(lblEmail);
		
		textField_6 = new JTextField();
		textField_6.setColumns(10);
		textField_6.setBounds(515, 92, 240, 20);
		panel.add(textField_6);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(null, "Options", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 175, 143, 338);
		frame.getContentPane().add(panel_1);
		
		JButton button = new JButton("Search");
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				CustomerCtr ctrCust=new CustomerCtr();
				
				if(textField.getText().equals("")!=true)
				{
					String stringID=textField.getText();
					System.out.println(stringID);
					int id=Integer.parseInt(stringID);
					Customer customer=ctrCust.getCustomerByID(id);
					putValuesOnTheScreen(customer);
					
					DefaultTableModel tdm=(DefaultTableModel)table.getModel();
					tdm.getDataVector().removeAllElements();
					tdm.fireTableDataChanged();
				}
				else
				{
					if(textField_1.getText().equals("")!=true)
					{
						String name=textField_1.getText();
						System.out.println(name);
						Customer customer=ctrCust.getCustomerByName(name);
						putValuesOnTheScreen(customer);
						
						DefaultTableModel tdm=(DefaultTableModel)table.getModel();
						tdm.getDataVector().removeAllElements();
						tdm.fireTableDataChanged();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Please insert either id or name for the supplier.", "Error!", JOptionPane.ERROR_MESSAGE);
					}
					
					DefaultTableModel tdm=(DefaultTableModel)table.getModel();
					tdm.getDataVector().removeAllElements();
					tdm.fireTableDataChanged();
				}
			}
		});
		button.setBounds(6, 16, 127, 35);
		panel_1.add(button);
		
		JButton button_1 = new JButton("Update");
		button_1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				CustomerCtr custCtr=new CustomerCtr();
				
				if(textField.getText().equals("")!=true)
				{
					String stringID=textField.getText();
					int custID=Integer.parseInt(stringID);
					
					Customer custObj1=custCtr.getCustomerByID(custID);
					if(checkCustomerExistence(custObj1)==true)
					{
						if(textField_1.getText().equals("")!=true || 
								textField_2.getText().equals("")!=true ||
								textField_3.getText().equals("")!=true ||
								textField_4.getText().equals("")!=true)
						{
							String custName=textField_1.getText();
							String custAddress=textField_2.getText();
							String custCity=textField_3.getText();
							
							String zipCode=textField_4.getText();
							int custZipCode=Integer.parseInt(zipCode) ;
							
							String custPhoneNo=textField_5.getText();
							String custEmail=textField_6.getText();
							
							custCtr.updateCustomer(custID, custName, custAddress, custZipCode, custCity, custPhoneNo, custEmail);
							
							clearValues();
							
							JOptionPane.showMessageDialog(null, "Customer has been successfully updated.", "Information", JOptionPane.INFORMATION_MESSAGE);
						}
						else
						{
							JOptionPane.showMessageDialog(null, "Some fields may be empty!", "Error!", JOptionPane.ERROR_MESSAGE);
						}
					}
					else
					{
						JOptionPane.showMessageDialog(null, "There is no such customer!", "Error!", JOptionPane.ERROR_MESSAGE);
					}
				}
				else
				{
					if(textField_1.getText().equals("")!=true)
					{
						String custName=textField_1.getText();
						
						Customer custObj2=custCtr.getCustomerByName(custName);
						if(checkCustomerExistence(custObj2)==true)
						{
							if(textField_1.getText().equals("")!=true || 
									textField_2.getText().equals("")!=true ||
									textField_3.getText().equals("")!=true ||
									textField_4.getText().equals("")!=true)
							{
								int custID=custObj2.getID();
								String name=textField_1.getText();
								String custAddress=textField_2.getText();
								String custCity=textField_3.getText();
								
								String zipCode=textField_4.getText();
								int custZipCode=Integer.parseInt(zipCode) ;
								
								String custPhoneNo=textField_5.getText();
								String custEmail=textField_6.getText();
								
								custCtr.updateCustomer(custID, name, custAddress, custZipCode, custCity, custPhoneNo, custEmail);
								
								clearValues();
								
								JOptionPane.showMessageDialog(null, "Customer has been successfully updated.", "Information", JOptionPane.INFORMATION_MESSAGE);
							}
							else
							{
								JOptionPane.showMessageDialog(null, "Some fields may be empty!", "Error!", JOptionPane.ERROR_MESSAGE);
							}
						}
						else
						{
							JOptionPane.showMessageDialog(null, "There is no such customer!", "Error!", JOptionPane.ERROR_MESSAGE);
						}
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Please insert either id or name for the supplier.", "Error!", JOptionPane.ERROR_MESSAGE);
					}
				}
				
				DefaultTableModel tdm=(DefaultTableModel)table.getModel();
				tdm.getDataVector().removeAllElements();
				tdm.fireTableDataChanged();
			
			}
		});
		button_1.setBounds(6, 108, 127, 35);
		panel_1.add(button_1);
		
		JButton button_2 = new JButton("Delete");
		button_2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				CustomerCtr custCtr=new CustomerCtr();
				
				if(textField.getText().equals("")!=true)
				{
					String stringID=textField.getText();
					int custId=Integer.parseInt(stringID);
					
					Customer customer1=custCtr.getCustomerByID(custId);
					if(checkCustomerExistence(customer1)==true)
					{
						custCtr.deleteCustomerByID(custId);
						
						clearValues();
						
						JOptionPane.showMessageDialog(null, "Customer has been deleted from the system.", "Information", JOptionPane.INFORMATION_MESSAGE);
					}
					else
					{
						JOptionPane.showMessageDialog(null, "There is no such customer!", "Error!", JOptionPane.ERROR_MESSAGE);
					}
				}
				else
				{
					if(textField_1.getText().equals("")!=true)
					{
						String custName=textField_1.getText();
						Customer customer2=custCtr.getCustomerByName(custName);
						int id=customer2.getID();
						if(checkCustomerExistence(customer2)==true)
						{
							custCtr.deleteCustomerByID(id);
							
							clearValues();
							
							JOptionPane.showMessageDialog(null, "Customer has been deleted from the system.", "Information", JOptionPane.INFORMATION_MESSAGE);
						}
						else
						{
							JOptionPane.showMessageDialog(null, "There is no such customer!", "Error!", JOptionPane.ERROR_MESSAGE);
						}
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Please insert either id or name for the customer.", "Error!", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		button_2.setBounds(6, 154, 127, 35);
		panel_1.add(button_2);
		
		JButton btnGetOrders = new JButton("Get orders");
		btnGetOrders.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				CustomerCtr custCtr=new CustomerCtr();
				if(textField.getText().equals("")==true)
				{
					JOptionPane.showMessageDialog(null, "Please insert id of the customer.", "Error!", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					String stringID=textField.getText();
					int custID=Integer.parseInt(stringID);
					Customer customer1=custCtr.getCustomerByID(custID);
					putValuesOnTheScreen(customer1);
					if(textField_1.getText().equals("")!=true)
					{
						OrderCtr orderCtr=new OrderCtr();
						ArrayList<SalesOrder> custOrderList=new ArrayList<SalesOrder>();
						custOrderList=orderCtr.findAllCustomerOrders(custID);
						
						DefaultTableModel model2 =new DefaultTableModel()
						{
							private static final long serialVersionUID = 1L;

							@Override
							public boolean isCellEditable(int row, int column)
							{
								//all cells false
								return false;
							}
						};
						
						//table heads
						String[] COLUMN_NAMES = {"OrderID", "Order date", "Delivery status", "Delivery date",  "Invoice number"};
						model2.setColumnIdentifiers(COLUMN_NAMES);
						
						//table values
						try
						{
							for(SalesOrder custOrder : custOrderList)
							{
								model2.addRow(new String[]
								{
									String.valueOf(custOrder.getId()),
									custOrder.getOrderDate(),
									custOrder.getDeliveryStatus(),
									custOrder.getDeliveryDate(),
									String.valueOf(custOrder.getInvoice().getInvoiceNo())
								});
							}
							table.setModel(model2);
						}
						
						catch(Exception ex)
						{
							System.out.println("Exception: " + ex);
						}
					}
					else
					{
						JOptionPane.showMessageDialog(null, "There is no such customer!", "Error!", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		btnGetOrders.setBounds(6, 246, 127, 35);
		panel_1.add(btnGetOrders);
		
		JButton button_4 = new JButton("Insert");
		button_4.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(textField_1.getText().equals("")==true || textField_2.getText().equals("")==true ||
				textField_3.getText().equals("")==true || textField_4.getText().equals("")==true)
				{
					JOptionPane.showMessageDialog(null, "Please insert all necessary data!", "Error!", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					CustomerCtr custCtr=new CustomerCtr();
					String custName=textField_1.getText();
					String custAddress=textField_2.getText();
					String custCity=textField_3.getText();
					String stringZipCode=textField_4.getText();
					int custZipCode=Integer.parseInt(stringZipCode);
					String custPhoneNo=textField_5.getText();
					String custEmail=textField_6.getText();
					
					custCtr.insertCustomer(custName, custAddress, custZipCode, custCity, custPhoneNo, custEmail);
					
					DefaultTableModel tdm=(DefaultTableModel)table.getModel();
					tdm.getDataVector().removeAllElements();
					tdm.fireTableDataChanged();
					
					JOptionPane.showMessageDialog(null, "Customer has been inserted into the table.", "Information", JOptionPane.INFORMATION_MESSAGE);	
				}
			}
		});
		button_4.setBounds(6, 62, 127, 35);
		panel_1.add(button_4);
		
		JButton button_5 = new JButton("Clear");
		button_5.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				clearValues();
			}
		});
		button_5.setBounds(6, 200, 127, 35);
		panel_1.add(button_5);
		
		JButton btnGetProducts = new JButton("Get customers");
		btnGetProducts.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					table.setModel(returnModel1());
				}
				catch(Exception ex)
				{
					System.out.println("Exception: " + ex);
				}
			}
		});
		btnGetProducts.setBounds(6, 292, 127, 35);
		panel_1.add(btnGetProducts);
		
		JButton button_7 = new JButton("Close");
		button_7.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				clearValues();
				frame.dispose();
			}
		});
		button_7.setBounds(785, 11, 155, 47);
		frame.getContentPane().add(button_7);
		
		JButton button_8 = new JButton("Minimize");
		button_8.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				frame.setState(JFrame.ICONIFIED);
			}
		});
		button_8.setBounds(785, 69, 155, 47);
		frame.getContentPane().add(button_8);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(163, 174, 777, 344);
		frame.getContentPane().add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setFillsViewportHeight(true);
		frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
	}
	
	
	public void clearValues()
	{
		textField.setText(null);
		textField_1.setText(null);
		textField_2.setText(null);
		textField_3.setText(null);
		textField_4.setText(null);
		textField_5.setText(null);
		textField_6.setText(null);
		
		DefaultTableModel tdm=(DefaultTableModel)table.getModel();
		tdm.getDataVector().removeAllElements();
		tdm.fireTableDataChanged();
	}
	
	public void putValuesOnTheScreen(Customer custObj)
	{
		textField.setText(String.valueOf(custObj.getID()));
		textField_1.setText(custObj.getName());
		textField_2.setText(custObj.getAddress());
		textField_3.setText(custObj.getCity());
		textField_4.setText(String.valueOf(custObj.getzipCode()));
		textField_5.setText(custObj.getPhone());
		textField_6.setText(custObj.getEmail());
	}
	
	public boolean checkCustomerExistence(Customer custObj)
	{
		boolean check=false;
		
		int custID=custObj.getID();
		String custName=custObj.getName();
		String custAddress=custObj.getAddress();
		int custZipCode=custObj.getzipCode();
		String custCity=custObj.getCity();
		
		String id=String.valueOf(custID);
		String zipCode=String.valueOf(custZipCode);
		
		if(id.equals("")==true || custName.equals("")==true || custAddress.equals("")==true ||
			zipCode.equals("")==true || custCity.equals("")==true )
		{
			check=false;
		}
		else
		{
			check=true;
		}
		return check;
	}
	
	private DefaultTableModel returnModel1()
	{
		DefaultTableModel model1=new DefaultTableModel()
		{
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column)
			{
				//all cells false
				return false;
			}
		};
		
		//table head
		String[] COLUMN_NAMES = {"CustID", "Name", "Address", "Zipcode", "City", "Phone", "E_Mail"};
		model1.setColumnIdentifiers(COLUMN_NAMES);
		
		CustomerCtr custCtr=new CustomerCtr();
		ArrayList<Customer> custList=new ArrayList<Customer>();
		custList=custCtr.getAllCustomers();
		
		//table values
		for(Customer customer : custList)
		{
			model1.addRow(new String[]
					{
					
						String.valueOf(customer.getID()),
						customer.getName(),
						customer.getAddress(),
						String.valueOf(customer.getzipCode()),
						customer.getCity(),
						customer.getPhone(),
						customer.getEmail()
						
					});
		}
		return model1;
	}
}